package com.jyniubi.gmall.cart.controller;

import com.jyniubi.gmail.common.bean.ResponseVo;
import com.jyniubi.gmail.common.exception.CartException;
import com.jyniubi.gmall.cart.entity.CartEntity;
import com.jyniubi.gmall.cart.interceptor.LoginInterceptor;
import com.jyniubi.gmall.cart.pojo.UserInfo;
import com.jyniubi.gmall.cart.service.CartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import javax.xml.ws.Response;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;


    /*@GetMapping("cart")
    @ResponseBody
    public String test(){
        UserInfo userInfo = LoginInterceptor.getUserInfo();
        System.out.println("userInfo:" + userInfo);
        return "hello cart";
    }*/

    /**
     *  查询购物车选中的总商品
     * @param userId
     * @return
     */
    @GetMapping("check/{userId}")
    @ResponseBody
    public ResponseVo<List<CartEntity>> queryCheckedCarts(@PathVariable("userId")Long userId){
        List<CartEntity> cartEntities = this.cartService.queryCheckedCarts(userId);
        return ResponseVo.ok(cartEntities);
    }

    /**
     * 合并登录状态和未登录状态的购物车，将合并后的购物车同步保存到redis 异步保存到mysql
     * 新增成功，然后跳转到成功加入该商品的购物车页面合并登录状态和未登录状态的购物车，
     * 将合并后的购物车同步保存到redis 异步保存到mysql 新增成功，然后跳转到成功加入该商品的购物车页面
     * @param cartEntity
     * @return
     */
    @GetMapping
    public String saveCart(CartEntity cartEntity){
        if (cartEntity == null || cartEntity.getSkuId() == null){
            throw new CartException("没有选择添加购物车的商品信息");
        }
        this.cartService.saveCart(cartEntity);
        return "redirect:http://cart.gmall.com/addCart.html?skuId=" + cartEntity.getSkuId() + "&count=" + cartEntity.getCount();
    }

    /**
     * 从redis中获取添加购物车的商品，注意数量是此次加入的数量，不是合并后的数量
     * @param cart
     * @param model
     * @return
     */
    @GetMapping("addCart.html")
    public String addCart(CartEntity cart,Model model) {
        BigDecimal count = cart.getCount();
        cart = this.cartService.queryCartById(cart.getSkuId());
        cart.setCount(count);
        model.addAttribute("cart",cart);
        return "addCart";
    }

    /**
     *  查询用户登录和未登录状态总的购物车
     * @param model
     * @return
     */
    @GetMapping("cart.html")
    public String queryCarts(Model model){
        List<CartEntity> carts = this.cartService.queryCart();
        model.addAttribute("carts",carts);
        return "cart";
    }

    /**
     * 更新购物车商品数量
     * @param cartEntity
     * @return
     */
    @PostMapping("updateNum")
    @ResponseBody
    public ResponseVo updateNum(@RequestBody CartEntity cartEntity){
        this.cartService.updateNum(cartEntity);
        return ResponseVo.ok();
    }

    /**
     * 删除购物车对应skuId的商品
     * @param skuId
     * @return
     */
    @PostMapping("deleteCart")
    @ResponseBody
    public ResponseVo deleteCart(@RequestParam("skuId") Long skuId){
        this.cartService.deleteCart(skuId);
        return ResponseVo.ok();
    }

    @GetMapping("test")
    @ResponseBody
    public String testAsync(){
        long now = System.currentTimeMillis();
        System.out.println("controller 方法执行");
        this.cartService.execute1();
        this.cartService.execute2();
        /*try {
            System.out.println("execute1 : " + execute1.get() + "execute2 : " + execute2.get());
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        System.out.println("执行时间 ：" + (System.currentTimeMillis() - now));
        return "hello cart...";
    }

}
